/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.pollaqi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.poll.pollaqi.dto.PollAqiExcelDTO;
import com.hopes.bizz.poll.pollaqi.entity.PollAqiEntity;
import com.hopes.bizz.poll.pollaqi.mapper.PollAqiMapper;
import com.hopes.bizz.poll.pollaqi.service.PollAqiService;
import com.hopes.bizz.poll.pollaqi.vo.PollAqiExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * AQI指数
 *
 * @author yumr
 * @date 2023-05-26 14:27:56
 */
@Service
public class PollAqiServiceImpl extends ServiceImpl<PollAqiMapper, PollAqiEntity> implements PollAqiService {


    private final String CODE_ERROR_MSG = "AQI污染级别不允许重复!";

    @Override
    public ValidateImportBO<PollAqiExcelDTO> validateImport(List<PollAqiExcelDTO> excelDTOList) {
        final ValidateImportBO<PollAqiExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<PollAqiExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = 0, failedNum = 0;

        final List<PollAqiEntity> list = this.list();
        for (PollAqiExcelDTO excelDTO : excelDTOList) {
            final boolean exist = list.stream().anyMatch(item -> excelDTO.getLevel().equals(item.getLevel()));
            if (exist) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<>() {
                    private static final long serialVersionUID = 554465332379609413L;

                    {
                        add(CODE_ERROR_MSG + " " + excelDTO.getName());
                    }
                }));
                failedNum++;
            } else {
                saveList.add(excelDTO);
                successNum++;
            }
        }
        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);

        return bo;
    }

    @Override
    public PollAqiEntity importDtoToEntity(PollAqiExcelDTO excelDTO) {
        final PollAqiEntity entity = new PollAqiEntity();
        BeanUtils.copyProperties(excelDTO, entity);
        entity.setOrgId(DictResolver.getItemValueByLabel(AdminDictEnum.sys_company, excelDTO.getOrgId(), Long::valueOf));
        return entity;
    }

    @Override
    public PollAqiExcelVO exportEntityToVo(PollAqiEntity entity) {
        final PollAqiExcelVO excelVO = new PollAqiExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setOrgId(DictResolver.getItemLabelByValue(AdminDictEnum.sys_company, entity.getOrgId()));
        return excelVO;
    }
}

